<?php

namespace app\admin\controller;

use app\common\controller\AdminBaseController;
use think\facade\Db;

class Auto extends AdminBaseController
{

    protected function initialize()
    {
        parent::initialize();
    }

    public function index(){
        $cfgPath = root_path().'auto'.DS.'cfg.json';
        if ($this->request->isAjax()){
            $param = $this->request->post();
            if(empty($param['count']) || empty($param['start_h']) || empty($param['start_m']) || empty($param['end_h']) || empty($param['end_m'])){
                return json(['msg'=>'参数错误','code'=>0]);
            }
            $param['count'] = intval($param['count']);
            $insert = [
                'module' => 'dp_cyms',
                'count' => $param['count']
            ];
            Db::name('auto')->insert($insert,true);

            $param['start_h'] = intval($param['start_h']);
            $param['start_m']  = intval($param['start_m'] );
            $param['end_h'] = intval($param['end_h']);
            $param['end_m'] = intval($param['end_m']);
            if($param['start_h'] >= 24) $param['start_h'] = 8; 
            if($param['start_m'] >= 60) $param['start_m'] = 0; 
            if($param['end_h'] >= 24) $param['end_h'] = 19; 
            if($param['end_m'] >= 60) $param['end_m'] = 59; 

            $starttime = $param['start_h'].':'.$param['start_m'];
            $endtime = $param['end_h'].':'.$param['end_m'];

            $ts = strtotime($starttime);
            $te = strtotime($endtime);
            $tt = $te - $ts;
            $sleep = ceil($tt / $param['count']);
            $sleep_m = ceil($sleep / 2);
            $sleep_s = $sleep - $sleep_m;
            $sleep_b = $sleep + $sleep_m;

            $data = [
                'count' => $param['count'],
                'starttime' => $starttime,
                'endtime' => $endtime,
                'sleep_s' => $sleep_s,
                'sleep_b' => $sleep_b
            ];
            $json = json_encode($data);
            if(file_put_contents($cfgPath, $json)) 
            {
                // $command = "bash xiangmu_shell.sh > /dev/null 2>&1";
                //exec($command);
                return json(['msg'=>'设置成功','code'=>1]);
            }
            return json(['msg'=>'设置失败','code'=>0]);
        }
        if(file_exists($cfgPath)){
            $json = file_get_contents($cfgPath);
            $data = json_decode($json, true);
            $this->assign('data',$data);
        }
        return $this->fetch();
    }

    public function log(){
        $logPath = root_path().'auto'.DS.'log';
        $dirlist = $this->getDirContent($logPath);
        $this->assign('dirlist',$dirlist);

        $logFile = $this->request->get('logfile');
        if(empty($logFile)){
            $logFile = 'check.log';
        }
        $this->assign('logfile',$logFile);
        $info = $this->tail($logPath.DS.$logFile,10);
        $this->assign('info',$info);
        return $this->fetch();
    }

    private function getDirContent($path){
        if(!is_dir($path)){
          return false;
        }
        //readdir方法
        /* $dir = opendir($path);
        $arr = array();
        while($content = readdir($dir)){
          if($content != '.' && $content != '..'){
            $arr[] = $content;
          }
        }
        closedir($dir); */
       
        //scandir方法
        $arr = array();
        $data = scandir($path);
        foreach ($data as $value){
          if($value != '.' && $value != '..'){
            $arr[] = $value;
          }
        }
        return $arr;
      }

    private function tail($filename, $num)
    {
        if(!file_exists($filename)){
            return '';
        }
        if (!$fp = fopen($filename, 'r')) {
            echo "open file fail";
            return false;
        }
        $pos = -2;
        $eof = "";
        $str = "";
        while ($num > 0) {
            while ($eof != "\n") {//这里控制从文件的最后一行开始读
                if (!fseek($fp, $pos, SEEK_END)) {
                    $eof = fgetc($fp);
                    $pos--;
                } else {
                    break;
                }
            }
            $str .= fgets($fp);//读取一行数据
            $eof = "";
            $num--;
        }
        return $str;
    }

}